package com.yuxinmuyu.sports_management.controller.UserController;

import com.yuxinmuyu.sports_management.bean.entiy.SysUsers;
import com.yuxinmuyu.sports_management.exception.BusinessException;
import com.yuxinmuyu.sports_management.exception.R;
import com.yuxinmuyu.sports_management.service.UserService;
import com.yuxinmuyu.sports_management.utils.Md5Util;
import com.yuxinmuyu.sports_management.utils.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @Author 张钦栩
 * @Email 1784746527@qq.com
 * @Create 2024-06-23
 * @Description
 */
@RestController
@RequestMapping("/user")
@Api(value ="用户",tags = {"用户"})
public class UserController {
    @Resource
    UserService userService;

    @ApiOperation("修改密码")
    @PostMapping("/updatePwd")
    public R<?>updatePwd(@RequestBody @Validated UpdatePwdRequest request){
        if(!Objects.equals(request.getCheckPwd(),request.getNewPwd())){
            throw new BusinessException("确认密码与修改后密码不一致！");
        }else{
            SysUsers users=userService.getByID(UserUtil.getUserId());
            if(users.getUserPwd().equals(Md5Util.getMD5String(request.getOldPwd()))){
                users.setUserPwd(Md5Util.getMD5String(request.getNewPwd()));
                userService.update(users);
            }else{
                throw new BusinessException("旧密码不正确！");
            }
        }
        return R.success("修改成功！");
    }
}
